unit uEncdDecd;

interface

uses classes;

function StringMd5(const Str: string): string;

function StreamMd5(AStream: TStream; Count: integer = 0): string;

function BufferMd5(const Buffer; Count: integer): string;

function AesEncrytString(const Str, Psw: string): string;

function AesDecryptString(const Str, Psw: string): string;

function AesEncrytString256(const Str, Psw: string): string;

function AesDecryptString256(const Str, Psw: string): string;

implementation

uses DCPcrypt2, DCPmd5, DCPhaval, DCPrijndael, DCPsha512,sysutils,DCPsha256;


function StringMd5(const Str: string): string;
var
  h: TDCP_md5;
  d: array [1 .. 16] of Byte;
  i: integer;
begin
  h := TDCP_md5.Create(nil);
  h.Init;
  h.UpdateStr(Str);   
  h.Final(d);
  Result := '';
  for i := 1 to 16 do
    Result := Result + IntToHex(d[i], 2);
  h.Free;
end;

function StreamMd5(AStream: TStream; Count: integer = 0): string;
var
  h: TDCP_md5;
  d: array [1 .. 16] of Byte;
  i: integer;
begin
  AStream.Position := 0;
  h := TDCP_md5.Create(nil);
  h.Init;
  if Count = 0 then
    Count := AStream.Size;
  h.UpdateStream(AStream, Count);
  h.Final(d);
  Result := '';
  for i := 1 to 16 do
    Result := Result + IntToHex(d[i], 2);
  h.Free;
end;

function BufferMd5(const Buffer; Count: integer): string;
var
  h: TDCP_md5;
  d: array [1 .. 16] of Byte;
  i: integer;
begin
  h := TDCP_md5.Create(nil);
  h.Init;

  h.Update(Buffer, Count);
  h.Final(d);
  Result := '';
  for i := 1 to 16 do
    Result := Result + IntToHex(d[i], 2);
  h.Free;
end;

function AesEncrytString(const Str, Psw: string): string;
var
  DCP: TDCP_rijndael;
begin
  DCP := TDCP_rijndael.Create(nil);
  DCP.InitStr(Psw, TDCP_sha512);
  Result := DCP.EncryptString(Str);
  DCP.Free;
end;

function AesDecryptString(const Str, Psw: string): string;
var
  DCP: TDCP_rijndael;
begin
  DCP := TDCP_rijndael.Create(nil);
  DCP.InitStr(Psw, TDCP_sha512);
  Result := DCP.DecryptString(Str);
  DCP.Free;
end;

function AesEncrytString256(const Str, Psw: string): string;
var
  DCP: TDCP_rijndael;
begin
  DCP := TDCP_rijndael.Create(nil);
  DCP.InitStr(Psw, TDCP_sha256);
  Result := DCP.EncryptString(Str);
  DCP.Free;
end;

function AesDecryptString256(const Str, Psw: string): string;
var
  DCP: TDCP_rijndael;
begin
  DCP := TDCP_rijndael.Create(nil);
  DCP.InitStr(Psw, TDCP_sha256);
  Result := DCP.DecryptString(Str);
  DCP.Free;
end;

end.
